import group from './group';
import builder from './builder';
import language from './language';
import file from './file';
import translateItem from './translateItem';
import project from './project';
import serializeProject from './Serialize';
import deserializeProject from './deserialize';
import LICENSE from './LICENSE';
// import fs from 'fs';
if (window.require) {
  var fs = window.require('fs');
  var path = window.require('path');
}

const build = new builder();

class manager {
  public buildProject(): builder {
    return build;
  }
  public buildGroup(value: string): group {
    return new group(value);
  }
  public buildFile(value: string, type: string, translateItem: translateItem[]): file {
    return new file(value, type, translateItem);
  }
  public buildLanguage(rValue: number, nValue: number): language {
    return new language(rValue, nValue);
  }
  public buildLICENSE(value: number): string {
    return LICENSE[value];
  }
  //Project 序列化 JSON 数据
  public serialize(value: project): string {
    return serializeProject.serializeProject(value);
  }
  //Project 反序列化
  public deserialize(value: string): project {
    return deserializeProject.deserializeProject(value);
  }
  public writeFile(value: project) {
    fs.readFile('./project/projectList.json', { encoding: 'utf8' }, (err, data) => {
      if (err) throw new Error(err.message);
      let projectList = JSON.parse(data.toString());
      let list: any[] = projectList.list;
      let d = {
        projectName: value.getProjectName(),
        projectDescription: value.getProjectDescription(),
        author: value.getAuthor(),
        createDate: value.getCreateDate(),
      };
      list.push(d);
      projectList.list = list;
      fs.writeFile(
        './project/projectList.json',
        JSON.stringify(projectList),
        { encoding: 'utf8' },
        (err) => {
          if (err) throw err;
        }
      );
    });
    fs.writeFile(
      path.join('./project/projectList/', value.getProjectName() + '.json'),
      this.serialize(value),
      { encoding: 'utf8', flag: 'w' },
      (err) => {
        if (err) throw err;
      }
    );
  }
  public readFile(value: string): project {
    let data = fs.readFileSync(path.join(value), 'utf-8');
    return this.deserialize(data.toString());
  }
  public checkProject(value: project): boolean {
    try {
      if (value.getAuthor().length <= 0) {
        return false;
      }
      if (value.getProjectName() === '') {
        return false;
      }
      if (value.getProjectDescription() === '') {
        return false;
      }
      if (
        value.getLanguage().getRawLanguage() > 4 ||
        value.getLanguage().getNewLanguage() > 4
      ) {
        return false;
      }
      if (value.getFileGroup().getChild().length > 0) {
        let group = value.getFileGroup();
        for (let i = 0, size = value.getFileGroup().getChild().length; i < size; i++) {
          let file = group[i].getFile();
          if (file.length > 0) {
            for (let j of file) {
              let item = j.getFileContent();
              if (item.length > 0) {
                for (let k of item) {
                  if (k.getItemKey() === '') {
                    return false;
                  }
                  if (k.getRawText() === '') {
                    return false;
                  }
                  if (k.getLastEdit() == undefined) {
                    return false;
                  }
                }
              }
            }
          }
        }
      }
    } catch (error) {
      return false;
    }
    return true;
  }
}

const pluginManager = new manager();

export default pluginManager;
